<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    public function index()
    {

        $roles = Role::all();

        return view("admin.role.index", [
            "roles" => $roles,
            "pageName" => "角色列表",
        ]);
    }

    public function create()
    {
        return view("admin.role.create", [
            "pageName" => "添加角色",
        ]);
    }

    public function save(Request $request)
    {
        $role = new Role();
        $role->role_name = $request->post("role_name");
        $role->role_code = $request->post("role_code");
        $role->save();

        return redirect()->route("adminRoleIndex");

    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        $role = Role::destroy($id);

        return redirect()->route("adminRoleIndex");

    }

    public function edit(Request $request)
    {
        $id = $request->input("id");
        $role = Role::find($id);
        return view("admin.role.edit", [
            "role" => $role,
            "pageName" => "编辑角色",
        ]);

    }

    public function update(Request $request)
    {
        $id = $request->input("id");
        $role = Role::find($id);
        $role->role_name = $request->post("role_name");
        $role->role_code = $request->post("role_code");
        $role->save();
        return redirect()->route("adminRoleIndex");

    }
}
